<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var arr = function (a, b) {
      if (!a.length) {
        return null
      }
      //  方法移除数组的第一项
      let val = a.shift()
      //方法返回传入一个测试条件（函数）符合条件的数组第一个元素位置。
      let index = b.findIndex(item => item === val)
      let left = b.slice(0, index)
      let right = b.slice(index + 1)
      return {
        val,
        left: arr(a.slice(0, index), left),
        right: arr(a.slice(index), right)
      }
    };
    console.log(arr(a = [3, 9, 20, 15, 7], b = [9, 3, 15, 20, 7]));
  </script>
</body>

</html>